package com.supor.aiot.mine.ui.recipe;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.api.RecipeCreateImpl;
import com.supor.aiot.mine.bean.RecipeApplyBean;

public class RecipeCreateViewModel extends BaseViewModel {

    private MutableLiveData<Result> applyResult = new MutableLiveData<>();
    private MutableLiveData<Result> undoResult = new MutableLiveData<>();
    private MutableLiveData<Result> applyStatusResult = new MutableLiveData<>();


    public MutableLiveData<Result> getApplyResult() {
        return applyResult;
    }

    public MutableLiveData<Result> getUndoResult() {
        return undoResult;
    }

    public MutableLiveData<Result> getApplyStatusResult() {
        return applyStatusResult;
    }

    public RecipeCreateViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void apply(RecipeApplyBean recipeApplyBean) {
        RecipeCreateImpl.getInstance().addRecipeApply(recipeApplyBean).subscribe(result -> {
            if (result.isSuccess()) {
                applyResult.setValue(new Result.Success(result.data));
            } else {
                applyResult.setValue(new Result.Error("apply failed"));
            }
        }, throwable -> {
            Logc.e("apply:" + throwable.getMessage());
            applyResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    @SuppressLint("CheckResult")
    public void undoApply(String applyId) {
        RecipeCreateImpl.getInstance().deleteRecipeApply(applyId).subscribe(result -> {
            if (result.isSuccess()) {
                undoResult.setValue(new Result.Success(Constants.Value.SUCCESS));
            } else {
                undoResult.setValue(new Result.Error("undoApply failed"));
            }
        }, throwable -> {
            Logc.e("undoApply:" + throwable.getMessage());
            undoResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

//    @SuppressLint("CheckResult")
//    public void undoApply() {
//        RecipeCreateImpl.getInstance().getApplyAll().subscribe(result -> {
//            if (result.isSuccess()) {
//                List<RecipeApplyBean> recipeApplyBeans = result.data;
//                if (recipeApplyBeans != null && recipeApplyBeans.size() != 0) {
//                    undoApply(recipeApplyBeans.get(0).applyId);
//                }
//            } else {
//                applyStatusResult.setValue(new Result.Error("getApplyAll failed"));
//            }
//        }, throwable -> {
//            Logc.e("getApplyAll:" + throwable.getMessage());
//            undoResult.setValue(new Result.Error(new Exception(throwable)));
//        });
//    }



    @SuppressLint("CheckResult")
    public void getApplyStatus() {
        RecipeCreateImpl.getInstance().getApplyStatus().subscribe(result -> {
            if (result.isSuccess()) {
                applyStatusResult.setValue(new Result.Success(result.data));
            } else {
                applyStatusResult.setValue(new Result.Error("getApplyStatus failed"));
            }
        }, throwable -> {
            Logc.e("getApplyStatus:" + throwable.getMessage());
            //applyStatusResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }


}
